package cn.com.kh;

import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @projectName: ngsapitask-yngd
 * @package: cn.com.kh
 * @className: YNGSUtils
 * @author: LiJT
 * @description: TODO
 * @date: 2022-07-04 15:50
 * @version: 1.0
 */
public class YNGSUtils {
    private static final Log log = LogFactory.getLog(LogToDB.class);

    // 密码安全性校验规则
    public static String checkPassword(Map<String, Object> reqMap) {
        log.info("checkPassword : " + reqMap);
        Map<String, Object> rstMap = new HashMap<String, Object>();
        String IDNo = (String) reqMap.get("IDNo");
        String mobileNo = (String) reqMap.get("mobileNo");
        String newPwd = (String) reqMap.get("newPwd");
        String confirmNewPwd = (String) reqMap.get("confirmNewPwd");
        boolean flag = true;
        boolean pwdFlag = true;
        for (int i = 0; i < newPwd.length(); i++) {//判断是否相等
            if (newPwd.charAt(0) != newPwd.charAt(i)) {
                flag = false;
                break;
            }
        }
        for (int i = 0; i < newPwd.length(); i++) {//判断是否连续
            if (i > 0) {
                int num = Integer.parseInt(newPwd.charAt(i) + "");
                int num_ = Integer.parseInt(newPwd.charAt(i - 1) + "") + 1;
                if (num != num_) {
                    pwdFlag = false;
                    break;
                }
            }
        }
        if (newPwd.length() != 6) {
            return "密码不为6位";
        } else if (flag) {
            if (!KHUtil.isNull(confirmNewPwd) && !newPwd.equals(confirmNewPwd)) {
                return "两次密码不相同";
            } else if (flag) {
                return "密码不能为相同的数字";
            } else if (pwdFlag) {
                return "密码不能为连续数字";
            }

            return "密码校验通过";
        }
        return "密码校验通过";
    }

    // 调用省网统一接口工具类
    public static String callYNBossAPI(EliteBean eliteBean, String serviceCode, String reqContent, int timeOut) {
        long startTime = System.currentTimeMillis();
        String YNBossURL = "";
        String inJson = "";
        String resJson = "{\"status\":\"-1\",\"message\":\"callYNBossAPI转发调用Boss失败\"}";
        String dealLog = "";
        try {
            // 获取省网调用URL
            YNBossURL = KHDBConfig.getAPIURL(eliteBean, "YNBossURL");

            // 定义调用头文件
            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/x-www-form-urlencoded");

            // 获取省网调用基础配置参数
            String YNBossURLParam = KHDBConfig.getParamConfig(eliteBean, "YNBossURLParam");// 云南广电省网BossAPI交互参数:version|system|deptid|clientcode|clientpwd|citycode
            String[] YNBossURLParamArr = YNBossURLParam.split("\\|");
            String version = YNBossURLParamArr[0];// 版本编号
            String system = YNBossURLParamArr[1];// 外围系统标识
            String deptid = YNBossURLParamArr[2];// 操作员所在部门
            String clientcode = YNBossURLParamArr[3];// BOSS预先配置
            String clientpwd = YNBossURLParamArr[4];// BOSS预先配置，MD5加密32位的密文
            String ivrcitycode = YNBossURLParamArr[5];// 操作员所在地市
            log.debug("调用国网接口入参信息 URL:" + YNBossURL + " version:" + version + " system:" + system + "	deptid:" + deptid + " clientcode:" + clientcode + "	clientpwd:" + clientpwd + "	ivrcitycode:" + ivrcitycode);

            // 生成交互流水号-生成规则：clientcode+YYYYMMDD+8 位流水
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String date = simpleDateFormat.format(new Date());
            String unixTime = String.valueOf(System.currentTimeMillis());
            String requestid = clientcode.toLowerCase() + date + unixTime.substring(unixTime.length() - 8);
            log.debug("调用国网接口交互流水号:" + requestid);

            // 生成完整调用省网接口参数
            HashMap<String, String> paramHashMap = new HashMap<String, String>();
            paramHashMap.put("version", version);
            paramHashMap.put("system", system);
            paramHashMap.put("citycode", ivrcitycode);
            paramHashMap.put("deptid", deptid);
            paramHashMap.put("clientcode", clientcode);
            paramHashMap.put("clientpwd", clientpwd);
            paramHashMap.put("servicecode", serviceCode);
            paramHashMap.put("requestid", requestid);
            paramHashMap.put("requestContent", reqContent);

            // 将参数拼接为固定调用格式
            inJson = SplicingMap(paramHashMap, "&");
            log.debug("参数拼接为固定调用格式inJson:" + inJson);

            resJson = Util.doHttpPostUseHead(YNBossURL, inJson, "UTF-8", timeOut, 0, headMap);
            dealLog = "调用国网接口返回信息resJson:" + resJson;
            log.debug(dealLog);
        } catch (Exception e) {
            dealLog = "获取用户缴费历史详情接口处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (System.currentTimeMillis() - startTime);
            log.error(dealLog, e);
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "调用省网Boss统一接口工具方法", "callYNBossAPI", inJson, resJson, String.valueOf((System.currentTimeMillis() - startTime)), "调用URL:" + YNBossURL + System.lineSeparator() + dealLog);
        }

        return resJson;
    }

    // URL参数拼接
    public static String SplicingMap(Map<String, String> param, String Splicing) {
        TreeMap<String, String> paramTreeMap = new TreeMap<>(param);

        String prestr = "";

        for (Map.Entry<String, String> vo : paramTreeMap.entrySet()) {
            vo.getKey();
            vo.getValue();
            prestr = prestr + Splicing + vo.getKey() + "=" + vo.getValue();
        }
        prestr = prestr.substring(1);
        return prestr;
    }
}
